<?php

class Data extends AsbtractRestController {

	public function stats_get() {
		$auth_arr_resp = $this->checkUserAuth();
		if (isset($auth_arr_resp)) {
			$this->response($auth_arr_resp);
		}
		$id = $this->get("id");
		$time_period = $this->get("time_period");
		$time_dim = $this->get("time_dim");
		$networks = $this->get("networks");
		if (!($time_dim)) {
			$time_dim = 'day';
		}
		$val_arr_resp = $this->validateRequest($time_period,$time_dim,$networks);
		if (empty($val_arr_resp)) {
			if (!$networks) {
				$networks_arr = array('admob','chartboost');
			} else {
				$networks_arr = explode(";",$networks);
			}
			$calculated_arr = array();
			foreach ($networks_arr as $network) {
				switch ($network) {
					case 'admob':
						$arr_resp = $this->admobCalc($id,$time_period,$time_dim);
						if (!array_key_exists("error",$arr_resp) && array_key_exists("data",$arr_resp)) {
							$calculated_arr[] = $arr_resp['data'];
						} else {
							 return $this->response($arr_resp);
						}
					break;
					case 'chartboost':
						$arr_resp = $this->chartBoostCalc($id,$time_period,$time_dim);
						if (!array_key_exists("error",$arr_resp) && array_key_exists("data",$arr_resp)) {
							$calculated_arr[] = $arr_resp['data'];
						} else {
							return $this->response($arr_resp);
						}
					break;
					default:
						;
					break;
				}
			}
			
			
			$days = $this->getDates($time_period);
 			
			$calculator = new DataCalculator();
			$resp = array();
			return $this->response(MessagingHelper::AddToData($resp,$calculator->calc($calculated_arr, $days)));
		} else {
			return $this->response($val_arr_resp);
		}
	}
	
	
	private function admobCalc($id,$time_period,$time_dim) {
		$admboModel = new AdMobModel;
		$arr_resp = array();
		if (empty($arr_resp)) {
			$site_id_arr = array();
			if ($id) {
				array_push($site_id_arr, $id);
			} else {
				$arr_resp = $admboModel->getData('search',$this->email,$id,$time_period,$time_dim,array());
				$site_id_arr = $admboModel->getSiteIdsArr($arr_resp);
			}
			if (!empty($site_id_arr)) {
				$arr_resp = $admboModel->getData('statistics',$this->email,$site_id_arr,$time_period,$time_dim,array('date','revenue','ecpm'));
			}
		}
		return $arr_resp;
	}
	
	public function chartBoostCalc($id,$time_period,$time_dim) {
		$chartModel = new ChartboostModel;
		$arr_resp = array();
		if (empty($arr_resp)) {
			$site_id_arr = array();
			if ($id) {
				array_push($site_id_arr, $id);
			}
			$arr_resp = $chartModel->getData('statistics',$this->email,$site_id_arr,$time_period,$time_dim,array('Date','Money Earned','eCPM Earned'));
			if (!array_key_exists("error",$arr_resp) && array_key_exists("data",$arr_resp)) {
			}
		}
		return $arr_resp;
	}
	



	private function validateRequest($time_period,$time_dim,$networks) {
		$arr_resp = array();
	
		if (!($time_period)) {
			MessagingHelper::AddToError($arr_resp,ErrorMessgaes::$USER_ACOUNT_CODE, ErrorMessgaes::$USER_ACOUNT_PARAMS_MISSING_MSG);
		}
		
		if  ($time_period < 1 || $time_period > 180) {
			MessagingHelper::AddToError($arr_resp,ErrorMessgaes::$USER_ACOUNT_CODE, ErrorMessgaes::$USER_ACOUNT_PARAMS_WRONG_TIME_PERIOD_MSG);
		}
	
		if ($time_dim != 'day' && $time_dim != 'week' && $time_dim != 'month') {
			MessagingHelper::AddToError($arr_resp,ErrorMessgaes::$USER_ACOUNT_CODE, ErrorMessgaes::$USER_ACOUNT_PARAMS_WRONG_DIM_MSG);
		}
	
		if ($networks && !preg_match('/^((chartboost|admob)[;]?)+$/i',$networks)) {
			MessagingHelper::AddToError($arr_resp,ErrorMessgaes::$USER_ACOUNT_CODE, ErrorMessgaes::$USER_ACOUNT_PARAMS_WRONG_NETWORKS_MSG);
		}
		
		return ($arr_resp);
	}

	public function getDates($time_period) {
		$days = array();
		$date = DayHelper::getGmdate($time_period);
		$end_date = gmdate('Y-m-d'); // today
		while (strtotime($date) <= strtotime($end_date)) {
			$days[] = $date;
			$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
		}
		return $days;
	}
	
	/**
	 
	 	public function admob_get() {
		$arr_resp = $this->checkUserAuth();
		if (isset($arr_resp)) {
			$this->response($arr_resp);
		}
		$id = $this->get("id");
		$time_period = $this->get("time_period");
		$time_dim = $this->get("time_dim");

		if (!($time_dim)) {
			$time_dim = 'day';
		}
		
		$admboModel = new AdMobModel;
		$arr_resp = $this->validateRequest($time_period,$time_dim);
		if (empty($arr_resp)) {
			$site_id_arr = array();
			if ($id) {
				array_push($site_id_arr, $id);
			} else {
				$arr_resp = $admboModel->getData('search',$this->email,$id,$time_period,$time_dim,array());
				$site_id_arr = $admboModel->getSiteIdsArr($arr_resp);
			}
			if (!empty($site_id_arr)) {
				$arr_resp = $admboModel->getData('statistics',$this->email,$site_id_arr,$time_period,$time_dim,array('date','revenue','ecpm'));
				if (!array_key_exists("error",$arr_resp) && array_key_exists("data",$arr_resp)) {
				}
			}
		}
		return $this->response($arr_resp);
	}

	public function chartboost_get() {
		$arr_resp = $this->checkUserAuth();
		if (isset($arr_resp)) {
			$this->response($arr_resp);
		}
		$id = $this->get("id");
		$time_period = $this->get("time_period");
		$time_dim = $this->get("time_dim");

		if (!($time_dim)) {
			$time_dim = 'day';
		}

		$chartModel = new ChartboostModel;
		$arr_resp = $this->validateRequest($time_period,$time_dim);
		if (empty($arr_resp)) {
			$site_id_arr = array();
			if ($id) {
				array_push($site_id_arr, $id);
			}
			$arr_resp = $chartModel->getData('statistics',$this->email,$site_id_arr,$time_period,$time_dim,array('Date','Money Earned','eCPM Earned'));
			if (!array_key_exists("error",$arr_resp) && array_key_exists("data",$arr_resp)) {
			}
		}
		return $this->response($arr_resp);
	}
	 
	 */



}